package com.example.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
import com.example.entity.PageBean;
import com.example.entity.Worker;
import com.example.mapper.WorkerMapper;
import com.example.service.WorkerService;
import com.example.utils.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Service是在实现类里的
@Slf4j
@Service
public class WorkerServiceImpl implements WorkerService {
@Autowired
private WorkerMapper workerMapper;
@Autowired
   private JwtUtils jwtUtils;
    @Override
    public Worker login(Worker worker) {
        String password=DigestUtils.md5DigestAsHex(worker.getPassword().getBytes());//md5加密

        worker.setPassword(password);
        log.info("md5密码加密：{}",password);
        return workerMapper.findByUsernamePassword(worker);
    }
    @Override
    public Map<String,Object> getuUserinfo(String token)
    {
        Map<String, Object> claims = null;
        try {
            claims = jwtUtils.parseJWT(token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Integer workerid = (Integer) claims.get("id");
        Worker worker=workerMapper.selectById(workerid);
        if(worker != null){
            Map<String, Object> data = new HashMap<>();
            data.put("name",worker.getWorkerName());
            data.put("roles","admin");
            return data;
        }
        return null;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);
        List<Worker> pageList=workerMapper.selectByPage(name);
        log.info("分页查询...");
        Page<Worker> p=(Page<Worker>) pageList;
        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public void add(Worker worker) {

        worker.setCreateTime(LocalDateTime.now());
        worker.setUpdateTime(LocalDateTime.now());
        if(worker.getPassword()!=null && worker.getPassword()!=""){
            String password=DigestUtils.md5DigestAsHex(worker.getPassword().getBytes());//md5加密
            worker.setPassword(password);
        }
        else{
            String password=DigestUtils.md5DigestAsHex("123456".getBytes());//md5加密
            worker.setPassword(password);
        }
        workerMapper.insert(worker);

    }

    @Override
    public void delete(List<Integer> ids) {
        workerMapper.delete(ids);
    }

    @Override
    public Worker findById(Integer id) {
        Worker worker=workerMapper.selectById(id);
        return worker;
    }

    @Override
    public void updata(Worker worker) {
        if(worker.getPassword()!=null && worker.getPassword()!=""){
            String password=DigestUtils.md5DigestAsHex(worker.getPassword().getBytes());//md5加密
            worker.setPassword(password);
            log.info("md5密码加密：{}",password);
        }

        //跟新跟新时间
        worker.setUpdateTime(LocalDateTime.now());
        workerMapper.updata(worker);
    }
}
